Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created API, Rick and Morty #181

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

Nazar090
Copy link

@Nazar090 Nazar090 commented Aug 9, 2024

No description provided.

@Override
public CharacterDto getRandomCharacter() {
List<Character> characters = characterRepository.findAll();
if (characters.isEmpty()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove


@Service
@RequiredArgsConstructor
public class CharacterServiceImpl implements CharacterService {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't find service where you use
You must use public API (you should use REST API).

Comment on lines 29 to 38
HttpRequest httpRequest = HttpRequest.newBuilder()
.GET()
.uri(URI.create(URL))
.build();
HttpResponse<String> response = httpClient
.send(httpRequest, HttpResponse.BodyHandlers.ofString());

JSONObject jsonResponse = new JSONObject(response.body());
JSONArray resultsArray = jsonResponse.getJSONArray("results");

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this only gets 20 characters (one page), not all of them, use .next() to get next page

@Nazar090 Nazar090 requested a review from okuzan August 16, 2024 11:12
@Override
public CharacterDto getRandomCharacter() {
List<Character> characters = characterRepository.findAll();
Random random = new Random();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better extract random to a class level field, not to recreate it needlessly per each method invocation


@Mapper(config = MapperConfig.class)
public interface CharacterMapper {
CharacterDto toDo(Character character);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
CharacterDto toDo(Character character);
CharacterDto toDto(Character character);

Comment on lines 54 to 56
nextPageUrl =
jsonResponse.isNull("next") ?
null : jsonResponse.getJSONObject("info").getString("next");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't look nice tbh ,let's use objectMapper.readValue() to read JSON into DTOs

@Nazar090 Nazar090 requested a review from okuzan August 18, 2024 11:18
public class CharacterServiceImpl implements CharacterService {
private final CharacterRepository characterRepository;
private final CharacterMapper characterMapper;
private static final Random random = new Random();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static final Random random = new Random();
private final Random random = new Random();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants